const path = require('path');
const webpack = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin');

const { publicPath } = require('./constant');

const entryPath = path.resolve(__dirname, publicPath, 'src/app.jsx');

module.exports = {
  entry: {
    test: entryPath,
    'test.min': entryPath,
  },
  output: {
    library: {
      name: '[name].js',
      type: 'umd',
      umdNamedDefine: true,
    },
  },
  resolve: {
    extensions: ['.js', '.jsx', '.ts', '.tsx', '.less', '.css'],
    alias: {
      '@': path.resolve(__dirname, publicPath, 'src'),
    },
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx|ts|tsx)$/,
        // exclude: /node_modules/,
        // include: path.resolve(__dirname, publicPath, 'src'),
        use: [
          {
            loader: 'thread-loader',
            options: {
              // worker: 2,
            },
          },
          'babel-loader',
        ],
      },
      {
        test: /\.(jpe?g|png|gif|svg)$/,
        exclude: /node_modules/,
        generator: {
          filename: 'img/[hash][ext][query]'
        },
        type: 'asset',
        // use: [
        //   {
        //     loader: 'url-loader',
        //     options: {
        //       limit: 8 * 1024,
        //       name: '[name][hash].[ext]',
        //       outputPath: 'img/',
        //       // publicPath: 'http://www.mrgu.top',
        //     },
        //   },
          // {
          //   loader: 'image-webpack-loader',
          //   options: {
          //     mozjpeg: {
          //       progressive: true,
          //       quality: 65,
          //     },
          //     optipng: {
          //       enabled: false,
          //     },
          //     pngquant: {
          //       quality: '65-90',
          //       speed: 4,
          //     },
          //     // gifsicle: {
          //     //   interlaced: false,
          //     // },
          //     webp: {
          //       quality: 75,
          //     },
          //   },
          // },
        //],
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/,
        generator: {
          filename: 'img/[hash][ext][query]'
        },
        type: 'asset/resource',
        // use: {
        //   loader: 'url-loader',
        //   options: {
        //     limit: 8192,
        //     name: '[name][hash].[ext]',
        //     outputPath: 'font/',
        //   },
        // },
      },
      {
        test: /\.(le|c)ss$/,
        use: [
          {
            loader: MiniCssExtractPlugin.loader,
          },
          'css-loader',
          'postcss-loader',
          {
            loader: 'less-loader',
            options: {
              lessOptions: {
                javascriptEnabled: true, // https://github.com/ant-design/ant-motion/issues/44
              },
            },
          },
        ],
      },
    ],
    noParse(content) { // 不解析没有依赖项的library
      return /react|react-dom/.test(content);
    },
  },
  externals: {
    react: 'react',
    ReactDOM: 'react-dom',
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: 'css/[name].global.css',
    }),
    // new webpack.ProvidePlugin({ // 通过provide插件注入组件库到模块里
    //   $: 'jquery',
    //   jQuery: 'jquery',
    // }),
    new FriendlyErrorsWebpackPlugin(),
  ],
};
